package com.gxa.ehome.controller;

import com.gxa.ehome.busiCodeEnum.ErrorCode;
import com.gxa.ehome.exception.BusinessException;
import com.gxa.ehome.pojo.SysUser;
import com.gxa.ehome.service.SysUserService;
import com.gxa.ehome.util.MapUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

//类中和方法中都指定了method,那么对于register方法来说,两种方式都会有效
//建议不再类中声明method
@Component
@Controller
@RequestMapping(value = "/sysUser"/*,method = {RequestMethod.POST,RequestMethod.GET}*/)

public class SysUserController {



    //自动装配：通过当前目标bean的类型作为参考，到spring的
    //容器中寻找匹配类型的bean，如果找到就将目标容器中已有的匹配的
    // bean注入到成员身上，匹配规则：byType根据类型匹配
    //如果要按name/id匹配，需要@Qualifier（"sysUserService")



    public void login(){
        System.out.println("saada");
        //sysUserService.login("GL004","123456");
    }


    //可以声明多种url和多种请求方式,但是不建议
   //@RequestMapping(value = "/reg","/reg2",method = {RequestMethod.POST,RequestMethod.GET})
   //将请求类型通过注解来确定:@GetMapping 使用Get方式,其他三种类似
    //当参数是对象类型是,前端通过传递对象的属性值,使用@RequestBody来
    //完成对象的数据注入
    //前端传递的参数中name要喝目标对象的属性匹配
    //前端使用表单或ajax形式(vue/layui,以data形式添加参数,通过
    // json传递给后端),后端只能用post方式接收而不能用get
    //@GetMapping(value ="/reg" )
   @PostMapping(value ="/login1" )
   @ResponseBody
  public void register(@RequestBody SysUser sysUser){

     /*   System.out.println(sysUser);
       HashMap<String, String> objectObjectHashMap = new HashMap<>();

       boolean login = sysUserService.login(sysUser);
       if (login){
           objectObjectHashMap.put("code","0");
      }
      else
      objectObjectHashMap.put("code","1");*/

   }

    //当需要在jsp页面中访问从controller上获取的对象信息
   //如果不写名字,则默认会用对象名小写首字母
    @ModelAttribute("sysUser")
    public SysUser getSysuser(){
        SysUser sysUser = new SysUser();
        sysUser.setJobNumber("Gl009");
        sysUser.setId(0);
        return sysUser;
    }
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }





    @Resource(name="sysUserService")
    SysUserService sysUserService;
    // /get/id/1
    @GetMapping("/get/id/{id}")
    @ResponseBody
    public  Map<String,Object> get(@PathVariable("id") Integer id){
        SysUser sysUser = sysUserService.selectById(id);
        Map<String,Object> responseMap=new HashMap<>();
        responseMap.put("code",0);
        responseMap.put("data",sysUser);
        responseMap.put("msg","查询成功");
        return responseMap;
    }
    //@RequestMapping(value = "/login",method = RequestMethod.POST)

/*    @RequestParam注解中的参数: name/value:作用相同,给参数命名
    defaultValue:默认值
    required:是否必须(true表示不可为空值)*/
@PostMapping (value = "/login")
@ResponseBody
public Map login(@RequestParam("jobNumber") String jobNumber, @RequestParam("password")String password) {
    /*System.out.println("jobNumber" + jobNumber);
    System.out.println("password" + password);*/
    boolean login = sysUserService.login(jobNumber, password);
   // MapUtil mapUtil = new MapUtil();
    HashMap<String, Object> map = new HashMap<>();
    if (login) {
    map.put("code", "0");
        map.put("msg","登录成功");
        return map;

    } else {
       // mapUtil.setCode("1");
       // mapUtil.setMsg("登录失败！");
        //throw new IOException("系统发生未知异常");
        throw new BusinessException(ErrorCode.LOGIN_FAILED);
    }
}




    @PostMapping (value = "/login2")
    @ResponseBody
    public Map<String, Object> login(@RequestBody SysUser sysUser){
        System.out.println("jobNumber"+sysUser.getJobNumber());
        System.out.println("password"+sysUser.getPassword());
        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");
        return mapUtil.getValues();
  }



  //前端使用表单,后端使用@RequBody
    //适合接收不同对象类型的数据
    //需要前端传递contentType:"application/json;charset=UTF-8" /data:JSON.stringify(obj.field)
    @PostMapping (value = "/login3")
    @ResponseBody
    public Map<String, Object> login(@RequestBody Map<String,Object> map){
        System.out.println("map"+map);

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");
        return mapUtil.getValues();
    }
    @Transactional(readOnly=false)
    @PostMapping (value = "/insert")
    @ResponseBody
   public Map<String, Object> insert(@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        boolean byJobNumber = sysUserService.findByJobNumber(sysUser.getJobNumber());
        HashMap<String, Object> map = new HashMap<>();
        if (byJobNumber) {
          throw new BusinessException(ErrorCode.USERNAME_EXISTS);
        } else {
            // mapUtil.setCode("1");
            // mapUtil.setMsg("登录失败！");
            //throw new IOException("系统发生未知异常");
            sysUserService.save(sysUser);
            map.put("code", "0");
            map.put("msg","添加成功");
            return map;
        }
}
    @PostMapping(value = "/delAll")
    public Map<String,Object> delall(@RequestParam("del") String del){

        return null;
    }
   @RequestMapping(value = "selectAll",method = RequestMethod.GET)
   @ResponseBody
   /* @ApiImplicitParams({@ApiImplicitParam(name = "page",required=true,paramType="Integer"),
    @ApiImplicitParam(name = "limit",required = true,paramType = "Integer")})*/
    public Map<String, Object> getByPage(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit){
        List<SysUser> byPage = sysUserService.findByPage(page, limit);
        System.out.println(limit);
        System.out.println(page);
        System.out.println(byPage);
       // HashMap<String, Object> map = new HashMap<>();
        MapUtil mapUtil = new MapUtil();
       mapUtil.setCode("0");
       mapUtil.setMsg("查询成功");
       mapUtil.setData(byPage);
      // mapUtil.put("count",100);
       mapUtil.put("count",byPage.size());//数据总数


       return mapUtil.getValues();
    }
}
